use log::Level;
use ratatui::{
    layout::Alignment,
    prelude::{Constraint, Direction, Layout},
    style::{Color, Style},
    symbols,
    widgets::{
        Block, BorderType, Borders, HighlightSpacing, LineGauge, Row, Table, TableState, Tabs,
    },
    Frame,
};

use crate::tui::app::App;

pub trait TableData<'a> {
    type Data;
    type TableData;

    fn data(&self) -> &Self::Data;

    fn to_table_data(&self) -> Self::TableData;
}

/// Renders the user interface widgets.
pub fn render(app: &mut App, frame: &mut Frame<'_>) {
    let layouts = match app.downloading_row {
        Some(_) => Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3),
                Constraint::Length(frame.size().height - 6),
                Constraint::Length(3),
            ])
            .split(frame.size()),
        None => Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3),
                Constraint::Length(frame.size().height - 3),
            ])
            .split(frame.size()),
    };
    frame.render_widget(
        Tabs::new(vec!["自定义", "Civitai", "哩布哩布", "土司"])
            .highlight_style(Style::default().fg(Color::LightMagenta))
            .block(
                Block::default()
                    .border_type(BorderType::Plain)
                    .borders(Borders::ALL),
            )
            .select(app.current_tab)
            .divider(symbols::DOT),
        layouts[0],
    );
    if log::log_enabled!(Level::Trace) {
        log::trace!("当前行：{:?}", app.current_row);
    }
    let mut state = TableState::default().with_selected(Some(app.current_row as usize));
    frame.render_stateful_widget(
        Table::new(app.sd_cfg.to_table_data())
            .header(
                Row::new(vec![
                    "模型名",
                    "模型类型",
                    "基础模型",
                    "触发词",
                    "大小",
                    "来源",
                ])
                .bottom_margin(1),
            )
            .block(
                Block::default()
                    .title("模型")
                    .title_alignment(Alignment::Center)
                    .borders(Borders::ALL)
                    .border_type(BorderType::Plain),
            )
            .style(Style::default().fg(Color::White))
            .widths(&[
                Constraint::Length(50),
                Constraint::Length(10),
                Constraint::Length(10),
                Constraint::Length(10),
                Constraint::Length(10),
                Constraint::Length(10),
            ])
            .column_spacing(1)
            .highlight_spacing(HighlightSpacing::Always)
            .highlight_symbol(">>")
            .highlight_style(Style::default().fg(Color::LightCyan)),
        layouts[1],
        &mut state,
    );
    if app.downloading_row.is_some() {
        frame.render_widget(
            LineGauge::default()
                .block(Block::default().borders(Borders::ALL).title("下载"))
                .gauge_style(Style::default().fg(Color::LightCyan))
                .line_set(symbols::line::THICK)
                .ratio(app.downloading_progress),
            layouts[2],
        )
    }
}
